package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.ScoreMapper;
import com.example.demo.model.Book;
import com.example.demo.model.Score;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/27 10:43
 */
@Controller
public class ScoreController {
    @Autowired(required = false)
    private BookMapper bookMapper;
    @Autowired(required = false)
    private ScoreMapper scoreMapper;
    @GetMapping("score")
        public String score(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session)
{
    User user= (User) session.getAttribute("user");
    int bookid = Integer.parseInt(request.getParameter("bookid"));
    int core = Integer.parseInt(request.getParameter("core"));
    if(user!=null)
    {
        if(scoreMapper.selectOne(new QueryWrapper<Score>().eq("userid",user.getId()).eq("bookid",bookid))==null)
        {
            scoreMapper.insert(new Score(user.getId(),bookid,(double) core,new Date()));
        }else {
            scoreMapper.update(null,new UpdateWrapper<Score>().eq("userid",user.getId()).eq("bookid",bookid).set("core",core));
        }
    }
    return "forward:info";
}
    @PostMapping("score")
    public String toscorepage(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session)
    {
        User user= (User) session.getAttribute("user");
        List<Score> scores = scoreMapper.selectList(new QueryWrapper<Score>().eq("userid", user.getId()));
        ArrayList<Book> books = new ArrayList<>();
        for (Score score : scores) {
            Integer bookid = score.getBookid();
            Book book = bookMapper.selectOne(new QueryWrapper<Book>().eq("id", bookid));
            books.add(book);
        }
        model.addAttribute("user",user);
        model.addAttribute("books",books);
        model.addAttribute("scores",scores);
        return "score";
    }
    @PostMapping("updatescore")
    public String update(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session)
    {
        User user= (User) session.getAttribute("user");
        Double core = Double.parseDouble(request.getParameter("core"));
        if (Math.abs(core-5.0)>0.001)
        {
            return "forward:score";
        }
        Integer bookid = Integer.parseInt(request.getParameter("bookid"));

        scoreMapper.update(null,new UpdateWrapper<Score>().eq("userid",user.getId()).eq("bookid",bookid).set("core",core));

        return "forward:score";
    }

}
